var b2Vec2 = Box2D.Common.Math.b2Vec2,
    b2BodyDef = Box2D.Dynamics.b2BodyDef,
    b2Body = Box2D.Dynamics.b2Body,
    b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
	b2Fixture = Box2D.Dynamics.b2Fixture,
	b2World = Box2D.Dynamics.b2World,
	b2MassData = Box2D.Collision.Shapes.b2MassData,
	b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
	b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
	b2Listener = Box2D.Dynamics.b2ContactListener;

var _units = [];
var moveKeys = {'w' : 0, 'd' : 0, 's' : 0, 'a' : 0};
var kibo = new Kibo();
var ctx;

console.log(b2Body.prototype);

radians = function(deg){
	return  deg * Math.PI / 180;
};

$(document).ready(function () {
	
	var map = [
	  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	  [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
	  [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
	  [0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0],
	  [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
	  [0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0],
	  [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
	  [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];

    window.requestAnimFrame = (function(){
          return  window.requestAnimationFrame       || 
                  window.webkitRequestAnimationFrame || 
                  window.mozRequestAnimationFrame    || 
                  window.oRequestAnimationFrame      || 
                  window.msRequestAnimationFrame     || 
                  function(callback, element){
                    window.setTimeout(callback, 1000 / 60);
                  };
    })();
    
    var stats = new Stats();
    document.body.appendChild(stats.domElement);
    
    var canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");
  
    var world = new b2World(
		new b2Vec2(0, 0),
		true
    );
    
    var fixDef = new b2FixtureDef;
    fixDef.friction = 1;
    fixDef.restitution = 0;
    

    var bodyDef = new b2BodyDef;
    bodyDef.type = b2Body.b2_staticBody;
    
	for(var i = 0; i < map.length; i++) {
		for(var j = 0; j < map[i].length; j++) {
			if(map[i][j] === 1) {
				fixDef.shape = new b2PolygonShape;
				fixDef.shape.SetAsBox(23.5, 23.5);
				bodyDef.position.x = (j * 50) + 25;
				bodyDef.position.y = (i * 50) + 25;
				world.CreateBody(bodyDef).CreateFixture(fixDef);
			}
		}
	}
	
    bodyDef.type = b2Body.b2_dynamicBody;
	
	fixDef.shape = new b2PolygonShape;
	fixDef.shape.SetAsBox(20, 25);
	bodyDef.position.x = (7 * 50) + 25;
	bodyDef.position.y = (8 * 50) + 25;
	_units[0] = world.CreateBody(bodyDef);
	_units[0].CreateFixture(fixDef);
	_units[0].SetSleepingAllowed(false);
	
    //setup debug draw
    var debugDraw = new b2DebugDraw();
    debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
    debugDraw.SetFillAlpha(0.3);
    debugDraw.SetLineThickness(1.0);
    debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
    world.SetDebugDraw(debugDraw);
    
	var listener = new b2Listener;
	
	listener.BeginContact = function(contact) {
	}
	
	listener.EndContact = function(contact) {
	}
	
	world.SetContactListener(listener);
    

    function update() {
       world.Step(1 / 60, 10, 10);
       world.DrawDebugData();
       world.ClearForces();
     
       stats.update();
       requestAnimFrame(update);
    };

    requestAnimFrame(update);
    
});

kibo.up(['w', 'a', 's', 'd'], function() {
	moveKeys[kibo.lastKey()] = 0;
	if(moveKeys['w'] === 1 || moveKeys['d'] === 1 || moveKeys['s'] === 1 || moveKeys['a'] === 1)
		return;
	_units[0].SetLinearVelocity(new b2Vec2(0, 0));
});

kibo.down('w', function() {
	if(moveKeys[kibo.lastKey()] === 1)
		return;
	var x = _units[0].GetPosition().x;
	var y = _units[0].GetPosition().y;
	
	_units[0].SetLinearVelocity(new b2Vec2(0, -150));
	_units[0].SetAngle(radians(0));
	xd = x % 25;
	if(xd >= (25 / 2))
		x = x + (25 - xd);
	else 
		x = x - xd;
	_units[0].SetPosition(new b2Vec2(x, y));
	moveKeys[kibo.lastKey()] = 1;
});
kibo.down('s', function() {
	if(moveKeys[kibo.lastKey()] === 1)
		return;
	var x = _units[0].GetPosition().x;
	var y = _units[0].GetPosition().y;
	xd = x % 25;
	if(xd >= (25 / 2))
		x = x + (25 - xd);
	else 
		x = x - xd;
	
	_units[0].SetLinearVelocity(new b2Vec2(0, 150));
	_units[0].SetAngle(radians(180));
	_units[0].SetPosition(new b2Vec2(x, y));
	moveKeys[kibo.lastKey()] = 1;
});
kibo.down('a', function() {
	if(moveKeys[kibo.lastKey()] === 1)
		return;
	var x = _units[0].GetPosition().x;
	var y = _units[0].GetPosition().y;
	yd = y % 25;
	if(yd >= (25 / 2))
		y = y + (25 - yd);
	else 
		y = y - yd;
	
	_units[0].SetLinearVelocity(new b2Vec2(-150, 0));
	_units[0].SetAngle(radians(270));
	_units[0].SetPosition(new b2Vec2(x, y));
	moveKeys[kibo.lastKey()] = 1;
});
kibo.down('d', function() {
	if(moveKeys[kibo.lastKey()] != 0)
		return;
	var x = _units[0].GetPosition().x;
	var y = _units[0].GetPosition().y;
	yd = y % 25;
	if(yd >= (25 / 2))
		y = y + (25 - yd);
	else 
		y = y - yd;
	
	_units[0].SetLinearVelocity(new b2Vec2(150, 0));
	_units[0].SetAngle(radians(90));
	_units[0].SetPosition(new b2Vec2(x, y));
	moveKeys[kibo.lastKey()] = 1;
});
